package com.uenpay.im.ui.old.activity.publicnumber;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author SL
 * @created 2016-1-19 下午3:46:36
 * @copyRight http://www.qiyunxin.com
 * @function 序列化公众号菜单
 */
public class SerializerPublicNumMenu {

	public static PublicNumMenu desPublicNumMenu(JSONObject json) {
		PublicNumMenu publicNumMenu = new PublicNumMenu();
		publicNumMenu.text = json.optString("text");
		publicNumMenu.url = json.optString("url");
		return publicNumMenu;
	}

	public static ArrayList<PublicNumMenu> desPublicNumMenus(String data) {
		JSONObject dataJson = null;
		ArrayList<PublicNumMenu> arrayList = new ArrayList<PublicNumMenu>();
		try {
			dataJson = new JSONObject(data);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		if (dataJson == null) {
			return arrayList;
		}

		JSONArray MenuArr = dataJson.optJSONArray("menu");
		if (MenuArr == null || MenuArr.length() < 1) {
			return arrayList;
		}
		for (int i = 0, length = MenuArr.length(); i < length; i++) {
			JSONObject childMenuJson = MenuArr.optJSONObject(i);
			PublicNumMenu numEntity = new PublicNumMenu();
			numEntity.text = childMenuJson.optString("text");

			if (childMenuJson.has("url")) {
				numEntity.url = childMenuJson.optString("url");
			}
			if (childMenuJson.has("menu")) {
				JSONArray arr = childMenuJson.optJSONArray("menu");
				if (arr != null && arr.length() > 0) {
					for (int j = 0, size = arr.length(); j < size; j++) {
						numEntity.arrayList.add(desPublicNumMenu(arr
								.optJSONObject(j)));
					}
				}
			}
			arrayList.add(numEntity);
		}
		return arrayList;
	}
}
